package com.ruoyi.web.controller.property.garbage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.GarbageType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.GarbageTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/8/30
 **/
@RestController
@RequestMapping("/property/garbage/garbage")
public class GarbageController extends BaseController {
    @Resource
    private GarbageTypeService garbageTypeService;

    @Log(title = "垃圾导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GarbageType garbageType)
    {
        List<GarbageType> list = garbageTypeService.selectGarbageTypeList(garbageType);
        ExcelUtil<GarbageType> util = new ExcelUtil<GarbageType>(GarbageType.class);
        util.exportExcel(response, list, "垃圾分类");
    }

    @GetMapping("/list")
    public TableDataInfo list(GarbageType garbageType)
    {
        startPage();
        List<GarbageType> list = garbageTypeService.selectGarbageTypeList(garbageType);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        List<GarbageType> list = garbageTypeService.selectAll();
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(garbageTypeService.selectById(id));
    }

    @Log(title = "垃圾分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody GarbageType garbageType)
    {
        garbageType.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = garbageTypeService.createGarbageType(garbageType);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "垃圾分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GarbageType garbageType) {
        garbageType.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = garbageTypeService.updateGarbageType(garbageType);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "垃圾分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(garbageTypeService.deleteGarbageTypeById(id));
    }
}
